package auctionhaus

import static auctionhaus.Bid.*

/**
 * Created by IntelliJ IDEA.
 * User: Todd
 * Date: 2/9/12
 * Time: 4:51 PM
 * To change this template use File | Settings | File Templates.
 */
class BidIntegrationTests extends GroovyTestCase {

    //This is to verify that the dateCreated is automatically set and never null after the Bid is saved
    //since the dateCreated field cannot have a nullable : false constraint
    void testDateCreatedSetOnSave() {
        def alice = new Customer(emailAddress: 'alice@umn.edu', password: 'password')
        alice.save(failOnError: true)
        def bob = new Customer(emailAddress: 'bob@umn.edu', password: 'password')
        bob.save(failOnError: true)

        def listing = new Listing(name: 'bicycle', endDateTime: new Date() + 5, seller: alice, startingBidAmount: 100)
        listing.bids = new HashSet<Bid>()
        listing.save(failOnError: true)

        def bid = new Bid(bidder: bob, listing: listing , amount: 103)
        bid.save()
        assert null != bid.dateCreated
    }

    void testListingCascadeSave() {
        def alice = new Customer(emailAddress: 'alice@umn.edu', password: 'password')
        alice.save(failOnError: true)
        def bob = new Customer(emailAddress: 'bob@umn.edu', password: 'password')
        bob.save(failOnError: true)
        def carl = new Customer(emailAddress: 'carl@umn.edu', password: 'password')
        carl.save(failOnError: true)

        def listing = new Listing(name: 'bicycle', endDateTime: new Date() + 5, seller: alice, startingBidAmount: 100)
        listing.save(failOnError: true, flush: true)

        def bid = new Bid(bidder: bob, listing: listing , amount: 103)
        listing.addToBids(bid)
        listing.save(failOnError: true, flush: true)
        bid = new Bid(bidder: carl, listing: listing , amount: 104)
        listing.addToBids(bid)
        listing.save(failOnError: true, flush: true)
        bid = new Bid(bidder: bob, listing: listing , amount: 106)
        listing.addToBids(bid)
        listing.save(failOnError: true, flush: true)

        def bids = findAll("from Bid as b where b.bidder.emailAddress = ? and b.listing.name = ?", ['bob@umn.edu', 'bicycle'])
        assert bids.size() == 2

        bids = findAll("from Bid as b where b.bidder.emailAddress = ? and b.listing.name = ?", ['carl@umn.edu', 'bicycle'])
        assert bids.size() == 1
    }

    void testBid50CentsMore() {
        def alice = new Customer(emailAddress: 'alice@umn.edu', password: 'password')
        alice.save(failOnError: true)
        def bob = new Customer(emailAddress: 'bob@umn.edu', password: 'password')
        bob.save(failOnError: true)
        def carl = new Customer(emailAddress: 'carl@umn.edu', password: 'password')
        carl.save(failOnError: true)

        def listing = new Listing(name: 'bicycle', endDateTime: new Date() + 5, seller: alice, startingBidAmount: 100)
        listing.save(failOnError: true, flush: true)
        assert 0 == listing.errors.fieldErrorCount

        def bid = new Bid(bidder: bob, listing: listing , amount: 103)
        listing.addToBids(bid)
        listing.save(flush: true)
        assert 0 == listing.errors.fieldErrorCount
        bid = new Bid(bidder: carl, listing: listing , amount: 103.25)
        listing.addToBids(bid)
        listing.save(flush: true);
        assert 1 == listing.errors.fieldErrorCount
        assert listing.errors.fieldErrors.get(0).code == 'increasetoosmall'
        listing.removeFromBids(bid)

        bid = new Bid(bidder: bob, listing: listing , amount: 106)
        listing.addToBids(bid)
        listing.save(flush: true)
        assert 0 == listing.errors.fieldErrorCount

        def bids = findAll("from Bid as b where b.bidder.emailAddress = ? and b.listing.name = ?", ['bob@umn.edu', 'bicycle'])
        assert bids.size() == 2

        bids = findAll("from Bid as b where b.bidder.emailAddress = ? and b.listing.name = ?", ['carl@umn.edu', 'bicycle'])
        assert bids.size() == 0
    }
}
